﻿using MediatR;
using ProductionLine.API.Application.Command.Car;
using ProductionLine.Domain.Team;
using ProductionLine.ErrorCode;
using ProductionLine.Extensions;
using ProductionLine.Infrastructure.Interface;

namespace ProductionLine.API.Application.Handler.Car
{
    public class GetDepartmentUserHandler : IRequestHandler<GetDepartmentUserCommand, APIResult<List<UserDepartmentDTO>>>
    {
        IBaseRepository<DepartmentModel> departRepo;
        IBaseRepository<UserModel> userRepo;



        public GetDepartmentUserHandler(IBaseRepository<DepartmentModel> departRepo, IBaseRepository<UserModel> userRepo)
        {
            this.departRepo = departRepo;
            this.userRepo = userRepo;
        }

        public Task<APIResult<List<UserDepartmentDTO>>> Handle(GetDepartmentUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<UserDepartmentDTO>> res = new APIResult<List<UserDepartmentDTO>>();
            var depart = departRepo.GetAll();
            var user=userRepo.GetAll();
            var linq = from a in depart
                       join b in user on a.DepartmentId equals b.DepartmentId
                       select new UserDepartmentDTO
                       {
                           DepartmentId=a.DepartmentId,
                           DepartmentName=a.DepartmentName,
                           UserId=b.UserId,
                           UserName=b.UserName
                       };
            res.Msg = "查询用户部门成功";
            res.Data = linq.ToList();
            res.Code = APIEnum.Success;
            return Task.FromResult(res);
        }
    }
}
